﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;

namespace PocketMahjong
{
	public class Config
	{
		private static XmlSerializer _serializer = new XmlSerializer(typeof(Config));

		private static Config _instance;
		public static Config Instance
		{
			get 
			{
				if (_instance == null)
				{
					_instance = new Config();
					Load();
				}
				return _instance; 
			}
		}

		private bool _isRandom = true;
		public bool IsRandom
		{
			get { return _isRandom; }
			set { _isRandom = value; }
		}

		private string _layout = "Turtle";
		public string Layout
		{
			get { return _layout; }
			set { _layout = value; }
		}

		private static string FileName
		{
			get
			{
				return Assembly.GetExecutingAssembly().GetName().CodeBase + ".config";
			}
		}

		public static void Save()
		{
			using(var fs = new FileStream(FileName, FileMode.Create))
				_serializer.Serialize(fs, _instance);
		}

		public static void Load()
		{
			if (File.Exists(FileName))
				using (var fs = new FileStream(FileName, FileMode.Open))
					_instance = (Config)_serializer.Deserialize(fs);
		}
	}
}
